package com.douban.book.controller;

import com.douban.book.bean.Book;
import com.douban.book.service.CreateExcel;
import com.douban.book.service.Spider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * Created by Administrator on 2018/3/1.
 */
@Controller
@RequestMapping("/")
public class SpiderController {

    //请求爬取网页内容
    @RequestMapping("spider.do")
    public String spider(String url, String topic, HttpServletRequest request, Model model){

        /*url 是豆瓣网址*/
        /*topic 是类别 如编程 散文 小说等等*/
        Set<Book> set = new Spider().getResources(url, topic);
        for (Book book : set) {
            System.out.println(book);
        }
        String path = request.getServletContext().getRealPath("/");
        System.out.println(path);
        boolean bool = new CreateExcel().insertData(set,topic,path);
        System.out.println(bool);
        if (bool){
            model.addAttribute("result","spider is ok 请到tomcat root 下去找寻需要的excel文件");
        }else{
            model.addAttribute("result","spider is faliure");
        }
        return "index";
    }
}
